import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GameModeSelection extends JFrame {

    public GameModeSelection() {
        setTitle("方块消除游戏 - 选择模式");
        setSize(600, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        // 主面板使用BorderLayout
        JPanel mainPanel = new JPanel(new BorderLayout(20, 20));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        mainPanel.setBackground(new Color(240, 245, 250));

        // 顶部标题
        JLabel titleLabel = new JLabel("选择游戏模式", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 28));
        titleLabel.setForeground(new Color(50, 100, 150));
        mainPanel.add(titleLabel, BorderLayout.NORTH);

        // 中间内容面板
        JPanel contentPanel = new JPanel(new GridLayout(1, 2, 30, 0));
        contentPanel.setOpaque(false);

        // 左侧模式选择面板
        JPanel modePanel = new JPanel(new GridLayout(3, 1, 0, 20));
        modePanel.setOpaque(false);

        // 创建模式按钮
        JButton singleModeBtn = createModeButton("单人模式");
        JButton doubleModeBtn = createModeButton("创建房间");
        JButton tripleModeBtn = createModeButton("进入房间");
        singleModeBtn.setForeground(Color.black);
        doubleModeBtn.setForeground(Color.black);
        tripleModeBtn.setForeground(Color.black);
        // 添加按钮事件
        singleModeBtn.addActionListener(e -> startGame(1));
        doubleModeBtn.addActionListener(e -> startGame(2));
        tripleModeBtn.addActionListener(e -> startGame(3));

        modePanel.add(singleModeBtn);
        modePanel.add(doubleModeBtn);
        modePanel.add(tripleModeBtn);

        // 右侧规则面板
        JPanel rulePanel = new JPanel(new BorderLayout());
        rulePanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(new Color(180, 200, 220), 2),
                "游戏规则",
                javax.swing.border.TitledBorder.CENTER,
                javax.swing.border.TitledBorder.TOP,
                new Font("微软雅黑", Font.BOLD, 18),
                new Color(70, 150, 180)));
        rulePanel.setBackground(new Color(250, 253, 255));

        JTextArea ruleText = new JTextArea();
        ruleText.setText("点击相连的三个及以上同色方块进行消除\n\n" +
                "消除后上方方块下落，顶部生成新方块\n\n" +
                "多人模式中，积分高者获胜\n\n" +
                "若无可消除方块，游戏结束并自动消除剩余方块");
        ruleText.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        ruleText.setEditable(false);
        ruleText.setLineWrap(true);
        ruleText.setWrapStyleWord(true);
        ruleText.setBackground(new Color(250, 253, 255));
        ruleText.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));

        rulePanel.add(ruleText, BorderLayout.CENTER);

        // 将左右面板添加到内容面板
        contentPanel.add(modePanel);
        contentPanel.add(rulePanel);

        mainPanel.add(contentPanel, BorderLayout.CENTER);
        add(mainPanel);
    }

    private JButton createModeButton(String text) {
        JButton button = new JButton(text);
        button.setFont(new Font("微软雅黑", Font.BOLD, 20));

        // 根据按钮文本设置不同颜色
        switch(text) {
            case "单人模式":
                button.setBackground(new Color(46, 204, 113)); // 绿色

                break;
            case "创建房间":
                button.setBackground(new Color(231, 76, 60)); // 红色
                break;
            case "进入房间":
                button.setBackground(new Color(155, 89, 182)); // 紫色
                break;
            default:
                button.setBackground(new Color(52, 152, 219)); // 默认蓝色
        }

        button.setForeground(Color.WHITE);
        // ...其他设置...
        return button;
    }

    private void dryx(){
        // 这里可以启动你的游戏
        // 创建kxxxl实例
        new kxxxl();

        // 使用SwingWorker确保UI初始化完成后再执行倒计时
        SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
            @Override
            protected Void doInBackground() throws Exception {
                // 等待UI初始化完成
                Thread.sleep(500); // 短暂等待UI组件初始化

                // 执行倒计时
                for (int i = 59; i >= 0; i--) {
                    Thread.sleep(1000);
                    publish(i); // 发送进度到UI线程
                    kxxxl.b.setText("Timer:" + i);
                }
                return null;
            }



            @Override
            protected void done() {
                System.out.println("gameover");
                int yh = JOptionPane.showConfirmDialog(null,
                        "时间到", "游戏结束",
                        JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.QUESTION_MESSAGE);
            }
        };
        worker.execute();
    }
    private void cjfj(){




    }
    private void jrfj(){

    }
    private void startGame(int playerCount) {
        String zf="";
        switch (playerCount){
            case 1:
                zf="单人游戏";
                JOptionPane.showMessageDialog(this, "开始" + zf + "!");
                dryx();
                break;
            case 2:
                zf="创建房间";
                JOptionPane.showMessageDialog(this, "开始" + zf + "!");
                cjfj();
                break;
            case 3:
                zf="进入房间";
                JOptionPane.showMessageDialog(this, "开始" + zf + "!");
                jrfj();
                break;

        }


    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            new GameModeSelection().setVisible(true);
        });
    }



}